#!/bin/bash
################### 自动化安装k8s集群脚本 #####################
# 执行之前：
# 	1. 去除特殊字符：sed -i 's/\r//' install_k8s.sh
# 	2. 添加执行权限：chmod +x install_k8s.sh
# 执行命令：source install_k8s.sh
# 执行之后：
# 脚本说明：
######################################################
echo "----------开始初始化安装k8s环境----------------"
sleep 5
echo "----------1.关闭防火墙----------------"
systemctl stop firewalld
systemctl disable firewalld

echo "----------2.关闭selinux----------------"
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

echo "----------3.关闭sawp----------------"
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

echo "----------4.设置hostname----------------"
hostnamectl set-hostname k8s-master
#hostnamectl set-hostname k8s-node1
#hostnamectl set-hostname k8s-node2

echo "----------5.添加主机名----------------"
cat >> /etc/hosts << EOF
192.168.14.130 k8s-master 
192.168.14.131 k8s-node1 
192.168.14.132 k8s-node2
EOF

echo "----------6.IPv4 流量传递到 ipv6----------------"
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 生效
sysctl --system

echo "----------7.同步机器时间----------------"
yum -y install ntpdate
ntpdate time.windows.com
# 修改时区
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

sleep 5
echo "----------开始安装docker----------------"
sleep 5
echo "----------0.准备环境----------------"
# 安装yum工具类
yum -y install yum-utils
# 添加阿里云docker-ce仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
echo "准备环境OK"

echo "----------1.卸载旧版本docker----------------"
# 卸载旧版本docker（如果存在才执行，否则不执行）
yum -y remove docker*
echo "remove old version success!"

echo "----------2.安装docker(社区版)----------------"
# 也可以安装指定版本docker（docker版本要和k8s的匹配一致）
yum install -y docker-ce-20.10.2  docker-ce-cli-20.10.2 containerd.io
# 启动docker，并设置开机自启动
systemctl start docker
systemctl enable docker
# 配置阿里云镜像加速, 并修改cgroupdriver
cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": [
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
# 重新加载配置文件
systemctl daemon-reload
# 重启docker
systemctl restart docker
echo "install docker success!"

echo "----------4.测试docker是否安装成功----------------"
echo "docker --version: " $(docker --version)
echo "docker info: " $(docker info)


sleep 5
echo "----------开始安装kubelet kubeadm kubectl----------------"
sleep 5
echo "----------1.卸载旧版本kubelet kubeadm kubectl----------------"
# 卸载旧版本docker（如果存在才执行，否则不执行）
yum remove -y kubelet kubeadm kubectl
echo "remove old version success!"

echo "----------2.设置K8S的yum安装源----------------"
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

echo "----------3.安装指定版本kubelet kubeadm kubectl----------------"
yum install -y kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0

echo "----------4.启动kubelet----------------"
systemctl enable kubelet

echo "----------5.查看k8s的版本----------------"
kubectl version
